import request from '@/utils/request'

export const apiAutomationApi = {
  // ==================== 接口管理 ====================
  
  // 获取接口列表
  getInterfaces: (params = {}) => {
    return request({
      url: '/api/v1/api-automation/interfaces',
      method: 'get',
      params
    })
  },

  // 创建接口
  createInterface: (data) => {
    return request({
      url: '/api/v1/api-automation/interfaces',
      method: 'post',
      data
    })
  },

  // 获取接口详情
  getInterface: (id) => {
    return request({
      url: `/api/v1/api-automation/interfaces/${id}`,
      method: 'get'
    })
  },

  // 更新接口
  updateInterface: (id, data) => {
    return request({
      url: `/api/v1/api-automation/interfaces/${id}`,
      method: 'put',
      data
    })
  },

  // 删除接口
  deleteInterface: (id) => {
    return request({
      url: `/api/v1/api-automation/interfaces/${id}`,
      method: 'delete'
    })
  },

  // 测试接口
  testInterface: (data) => {
    return request({
      url: '/api/v1/api-automation/interfaces/test',
      method: 'post',
      data,
      timeout: 60000 // 60秒超时
    })
  },

  // ==================== 接口参数管理 ====================
  
  // 创建接口参数
  createParameter: (interfaceId, data) => {
    return request({
      url: `/api/v1/api-automation/interfaces/${interfaceId}/parameters`,
      method: 'post',
      data
    })
  },

  // 更新接口参数
  updateParameter: (id, data) => {
    return request({
      url: `/api/v1/api-automation/parameters/${id}`,
      method: 'put',
      data
    })
  },

  // 删除接口参数
  deleteParameter: (id) => {
    return request({
      url: `/api/v1/api-automation/parameters/${id}`,
      method: 'delete'
    })
  },

  // ==================== 接口断言管理 ====================
  
  // 创建接口断言
  createAssertion: (interfaceId, data) => {
    return request({
      url: `/api/v1/api-automation/interfaces/${interfaceId}/assertions`,
      method: 'post',
      data
    })
  },

  // 更新接口断言
  updateAssertion: (id, data) => {
    return request({
      url: `/api/v1/api-automation/assertions/${id}`,
      method: 'put',
      data
    })
  },

  // 删除接口断言
  deleteAssertion: (id) => {
    return request({
      url: `/api/v1/api-automation/assertions/${id}`,
      method: 'delete'
    })
  },

  // ==================== 测试场景管理 ====================
  
  // 获取场景列表
  getScenarios: (params = {}) => {
    return request({
      url: '/api/v1/api-automation/scenarios',
      method: 'get',
      params
    })
  },

  // 创建场景
  createScenario: (data) => {
    return request({
      url: '/api/v1/api-automation/scenarios',
      method: 'post',
      data
    })
  },

  // 获取场景详情
  getScenario: (id) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${id}`,
      method: 'get'
    })
  },

  // 更新场景
  updateScenario: (id, data) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${id}`,
      method: 'put',
      data
    })
  },

  // 删除场景
  deleteScenario: (id) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${id}`,
      method: 'delete'
    })
  },

  // ==================== 场景步骤管理 ====================
  // 步骤管理已废弃，使用流程图API替代

  // ==================== 场景执行 ====================
  
  // 执行场景
  executeScenario: (data) => {
    return request({
      url: '/api/v1/api-automation/scenarios/execute',
      method: 'post',
      data,
      timeout: 300000 // 5分钟超时
    })
  },

  // 调试场景
  debugScenario: (data) => {
    return request({
      url: '/api/v1/api-automation/scenarios/debug',
      method: 'post',
      data,
      timeout: 300000 // 5分钟超时
    })
  },

  // ==================== 执行记录管理 ====================
  
  // 获取执行记录列表
  getExecutions: (params = {}) => {
    return request({
      url: '/api/v1/api-automation/executions',
      method: 'get',
      params
    })
  },

  // 获取执行记录详情
  getExecution: (id) => {
    return request({
      url: `/api/v1/api-automation/executions/${id}`,
      method: 'get'
    })
  },

  // ==================== 环境配置管理 ====================
  
  // 获取环境配置列表
  getEnvironments: () => {
    return request({
      url: '/api/v1/api-automation/environments',
      method: 'get'
    })
  },

  // 创建环境配置
  createEnvironment: (data) => {
    return request({
      url: '/api/v1/api-automation/environments',
      method: 'post',
      data
    })
  },

  // 更新环境配置
  updateEnvironment: (id, data) => {
    return request({
      url: `/api/v1/api-automation/environments/${id}`,
      method: 'put',
      data
    })
  },

  // 删除环境配置
  deleteEnvironment: (id) => {
    return request({
      url: `/api/v1/api-automation/environments/${id}`,
      method: 'delete'
    })
  },

  // ==================== 流程图管理 ====================
  
  // 获取场景流程图
  getScenarioFlow: (scenarioId) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${scenarioId}/flow`,
      method: 'get'
    })
  },

  // 保存场景流程图
  saveScenarioFlow: (scenarioId, flowData) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${scenarioId}/flow`,
      method: 'post',
      data: flowData
    })
  },

  // 更新流程节点
  updateFlowNode: (scenarioId, nodeId, nodeData) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${scenarioId}/flow/nodes/${nodeId}`,
      method: 'put',
      data: nodeData
    })
  },

  // 删除流程节点
  deleteFlowNode: (scenarioId, nodeId) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${scenarioId}/flow/nodes/${nodeId}`,
      method: 'delete'
    })
  },

  // 执行流程图场景
  executeScenarioFlow: (scenarioId, data) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${scenarioId}/execute-flow`,
      method: 'post',
      data
    })
  },

  // 调试流程图场景
  debugScenarioFlow: (scenarioId, data) => {
    return request({
      url: `/api/v1/api-automation/scenarios/${scenarioId}/debug-flow`,
      method: 'post',
      data
    })
  }
}